package at.ac.univie.zsu.aguataplan.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import at.ac.univie.zsu.aguataplan.domain.DetectedEvent;
import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.Video;

/**
 * @author gerry
 * 
 */
@Repository
public class DetectedEventDaoBean implements DetectedEventDao {
	@PersistenceContext
	transient EntityManager em;

	@Autowired
	private Dao dao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DetectedEventDaoBean() {

	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	@Transactional
	public void cleanDetectedEvent() throws Exception {
		List<DetectedEvent> detectedEvents = dao.findAll(DetectedEvent.class);

		for (DetectedEvent d : detectedEvents) {
			em.remove(d);
		}

	}

	@Override
	public DetectedEvent addEventToDetectedEvent(DetectedEvent detectedEvent) throws Exception {
		dao.persist(detectedEvent);

		return detectedEvent;
	}

	@Override
	public DetectedEvent mergeDetectedEvent(DetectedEvent detectedEvent) throws Exception {
		DetectedEvent retDetectedEvent;
		retDetectedEvent = dao.merge(detectedEvent);

		return retDetectedEvent;
	}

	@Override
	public List<DetectedEvent> getDetectedEventList() throws Exception {
		List<DetectedEvent> detectedEvents;
		detectedEvents = em.createQuery("SELECT o FROM " + DetectedEvent.class.getName() + " o ORDER BY detTime")
				.getResultList();

		return detectedEvents;
	}

	@Override
	public List<DetectedEvent> getDetectedEventByVideo(Video video) throws Exception {
		List<DetectedEvent> detectedEvents;
		String query = "SELECT detectedEvent FROM " + DetectedEvent.class.getName()
				+ " AS detectedEvent JOIN detectedEvent.refVideo AS video WHERE video.id=" + video.getId()
				+ " ORDER BY detectedEvent.detTime";

		detectedEvents = em.createQuery(query).getResultList();

		return detectedEvents;
	}

	@Override
	public List<DetectedEvent> getDetectedEventListOrderByVideoAndDetection() throws Exception {
		List<DetectedEvent> detectedEvents;
		detectedEvents = em.createQuery(
				"SELECT detectedEvent FROM " + DetectedEvent.class.getName()
						+ " AS detectedEvent ORDER BY detectedEvent.refVideo asc, detectedEvent.detTime asc")
				.getResultList();

		return detectedEvents;
	}

	@Override
	public void deleteDetectedEvent(long id) throws Exception {
		dao.remove(DetectedEvent.class, id);
	}

	@Override
	public List<DetectedEvent> getDetectedEventByEventAndVideo(Event event, Video video) throws Exception {
		List<DetectedEvent> detectedEvents;
		String query = "SELECT detectedEvent FROM "
				+ DetectedEvent.class.getName()
				+ " AS detectedEvent JOIN detectedEvent.refEvent AS event JOIN detectedEvent.refVideo AS video WHERE event.id="
				+ event.getId() + " AND video.id=" + video.getId() + " ORDER BY detectedEvent.detTime";

		detectedEvents = em.createQuery(query).getResultList();

		return detectedEvents;
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

}
